package org.ssmd3.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.ssmd3.pojo.User;
import org.ssmd3.service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    @RequestMapping(value = "/login")
    public String login(){
        return "/login";
    }

    @RequestMapping(value = "/dologin")
    public String dologin(Model model, HttpSession session, User user){
        User user1=userService.getUser(user.getUserName());
        if (user1!=null){
            if (user.getUserPassword().equals(user1.getUserPassword())){
                //在session中添加用户状态信息
                session.setAttribute("uname",user1.getUserName());
                return "redirect:/user/index";
            }else{
                model.addAttribute("error","密码错误");
            }
        }else{
            model.addAttribute("error","用户名不存在");
        }


        return "/login";
    }
}
